#include "texteditor.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QLineEdit>
#include <QToolBar>
#include <QBoxLayout>
#include <QToolButton>
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include <QTime>
#include <QTextStream>
#include <QDebug>
#include <QFileInfo>
#include <qtextcodec.h>
#include <QList>
TextEditor::TextEditor(TabManager* tabManager) : tabManager(tabManager)
{
    find_replace = new QAction("替换",this);
    find_input = new QLineEdit;
    replace_input = new QLineEdit;
    find_cancel = new QAction("取消", this);
    find_start = new QAction("从头查找",this);

    find_next = new QAction("查找下一个", this);
    find_replace_all = new QAction("替换全部");
    find_replace_select = new QAction("替换当前项");
    find_replace_select->setEnabled(false);
    replace_cancel = new QAction("取消", this);
    find_input->setPlaceholderText("查找内容");
    //初始化查找栏与替换栏的按钮

    replace_button = new QToolButton();
    replace_button->setDefaultAction(find_replace);
    start_button = new QToolButton();
    start_button->setDefaultAction(find_start);
    next_button = new QToolButton();
    next_button->setDefaultAction(find_next);
    cancel_button = new QToolButton();
    cancel_button->setDefaultAction(find_cancel);
    spacer = new QWidget();
    spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    //摆放查找栏的按钮

    replace_input->setPlaceholderText("替换内容");
    replace_all_button = new QToolButton();
    replace_all_button->setDefaultAction(find_replace_all);
    replace_select_button = new QToolButton();
    replace_select_button->setDefaultAction(find_replace_select);
    replace_cancel_button = new QToolButton();
    replace_cancel_button->setDefaultAction(replace_cancel);
    spacer_ = new QWidget();
    spacer_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    //摆放替换栏的按钮

    connect(find_start,SIGNAL(triggered()),this,SLOT(on_find_start()));
    connect(find_input, &QLineEdit::textChanged, this, &TextEditor::on_find_text);
    connect(find_cancel, SIGNAL(triggered()), this, SLOT(on_find_cancel()));
    connect(find_next, SIGNAL(triggered()), this,SLOT(on_find_next()));
    connect(find_replace, SIGNAL(triggered()), this, SLOT(on_replace()));
    connect(find_replace_all, SIGNAL(triggered()), this, SLOT(on_replace_all()));
    connect(replace_cancel, SIGNAL(triggered()), this, SLOT(on_replace_cancel()));
    connect(find_replace_select, SIGNAL(triggered()), this, SLOT(on_replace_select()));
}
void TextEditor::on_copy()
{
    tabManager->text1->copy();
}
void TextEditor::on_paste()
{
    tabManager->text1->paste();
}
void TextEditor::on_cut()
{
    tabManager->text1->cut();
}
void TextEditor::on_selectAll()
{
    tabManager->text1->selectAll();
}

void TextEditor::on_find_and_replace()
{
    tabManager->mainWindow->find_toolbar->setVisible(true);
}

void TextEditor::on_find_cancel()
{
    tabManager->mainWindow->find_toolbar->setVisible(false);
    QTextCursor cursor_ = tabManager->text1->textCursor();
    cursor_.movePosition(QTextCursor::Start);
    cursor_.setPosition(0);
    cursor_.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
    QTextCharFormat format;
    if(tabManager->toolsSettings->currentTheme == "Dark")
        format.setForeground(QColor("white")); // 设置字体颜色
    else
        format.setForeground(QColor("black")); // 设置字体颜色
    cursor_.setCharFormat(format);
    find_input->clear();
    start_flag = false;
    find_start->setText("从头查找");
    find_next->setEnabled(false);
    find_replace_select->setEnabled(false);
    if(tabManager->mainWindow->replace_toolbar&&tabManager->mainWindow->replace_toolbar->isVisible())
    {
        tabManager->mainWindow->replace_toolbar->setVisible(false);
        replace_input->clear();
    }
    connect(tabManager->text1,SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
}
void TextEditor::on_find_text(const QString &text)
{
    tabManager->text1->blockSignals(true);
    //暂时阻隔文本变化的信号
    QString find_text = text;
    QTextCursor cursor = tabManager->text1->textCursor();
    cursor.movePosition(QTextCursor::Start);
    document = tabManager->text1->document();

    cursor.setPosition(0);
    cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
    QTextCharFormat format;
    if(tabManager->toolsSettings->currentTheme == "Dark")
        format.setForeground(QColor("white")); // 设置字体颜色
    else
        format.setForeground(QColor("black")); // 设置字体颜色
    cursor.setCharFormat(format);
    //使用光标先取消所有文本的高亮

    QTextCursor find_cursor(document);
    QTextCharFormat plainFormat(find_cursor.charFormat());
    QTextCharFormat colorFormat = plainFormat;

    colorFormat.setBackground(Qt::yellow);
    colorFormat.setFontWeight(QFont::Bold);

    while (!find_cursor.isNull() && !find_cursor.atEnd()) {
        find_cursor = document->find(find_text, find_cursor);
        if (!find_cursor.isNull()) {
            find_cursor.mergeCharFormat(colorFormat);
        }
    }
    //使查找的文本高亮
    tabManager->text1->blockSignals(false);
}
void TextEditor::on_find_start()
{
    tabManager->text1->blockSignals(true);
    //暂时阻隔文本变化的信号
    document = tabManager->text1->document();
    if(!start_flag)
    {
        start_cursor = QTextCursor(document);
        start_cursor.setPosition(0);
        find_start->setText("停止查找");
        start_flag = true;
        find_next->setEnabled(true);
        find_replace_select->setEnabled(true);
        QString find_text = find_input->text();
        QTextCursor cursor = tabManager->text1->textCursor();
        cursor.movePosition(QTextCursor::Start);
        cursor.setPosition(0);
        cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
        QTextCharFormat format;
        if(tabManager->toolsSettings->currentTheme == "Dark")
            format.setForeground(QColor("white")); // 设置字体颜色
        else
            format.setForeground(QColor("black")); // 设置字体颜色
        cursor.setCharFormat(format);
        QTextCharFormat plainFormat(start_cursor.charFormat());
        QTextCharFormat colorFormat = plainFormat;
        colorFormat.setBackground(Qt::yellow);
        colorFormat.setFontWeight(QFont::Bold);
        if (!start_cursor.isNull())
        {
            start_cursor = document->find(find_text, start_cursor);
            start_cursor.mergeCharFormat(colorFormat);
        }
        if(start_cursor.isNull())
        {
            QMessageBox::information(this,"提示","无更多匹配项");
            find_replace_select->setEnabled(false);
            find_next->setEnabled(false);
            return;
        }
    }
    else
    {
        start_flag = false;
        find_start->setText("从头查找");
        find_replace_select->setEnabled(false);
        find_next->setEnabled(false);
        on_find_text(find_input->text());
    }
    tabManager->text1->blockSignals(false);
}
void TextEditor::on_find_next()
{
    tabManager->text1->blockSignals(true);
    document = tabManager->text1->document();
    QString find_text = find_input->text();
    QTextCursor cursor = tabManager->text1->textCursor();
    cursor.movePosition(QTextCursor::Start);
    cursor.setPosition(0);
    cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
    QTextCharFormat format;
    if(tabManager->toolsSettings->currentTheme == "Dark")
        format.setForeground(QColor("white")); // 设置字体颜色
    else
        format.setForeground(QColor("black")); // 设置字体颜色
    cursor.setCharFormat(format);
    QTextCharFormat plainFormat(start_cursor.charFormat());
    QTextCharFormat colorFormat = plainFormat;
    colorFormat.setBackground(Qt::yellow);
    colorFormat.setFontWeight(QFont::Bold);
    if (!start_cursor.isNull())
    {
        start_cursor = document->find(find_text, start_cursor);
        start_cursor.mergeCharFormat(colorFormat);
    }
    if(start_cursor.isNull())
    {
        QMessageBox::information(this,"提示","无更多匹配项");
        find_replace_select->setEnabled(false);
        find_next->setEnabled(false);
        return;
    }
    tabManager->text1->blockSignals(false);
}
void TextEditor::on_text_changed_find()
{
    tabManager->text1->blockSignals(true);
    if(tabManager->mainWindow->find_toolbar->isVisible() && !start_flag)
    {
        on_find_text(find_input->text());
    }

    tabManager->text1->blockSignals(false);
}

//"查找"栏的功能函数
void TextEditor::on_replace()
{
    tabManager->mainWindow->replace_toolbar->setVisible(true);
}
void TextEditor::on_replace_cancel()
{
    tabManager->mainWindow->replace_toolbar->setVisible(false);
    replace_input->clear();
}
void TextEditor::on_replace_all()
{
    QString find_text = find_input->text();
    QString replace_text = replace_input->text();

    QString current_text = tabManager->text1->toPlainText();
    current_text.replace(find_text, replace_text);

    tabManager->text1->setPlainText(current_text);
}
void TextEditor::on_replace_select()
{
   QString replace_text = replace_input->text();
   start_cursor.removeSelectedText();
   start_cursor.insertText(replace_text);
}
//"替换"栏的功能函数
